package org.example.volunteerend.service.impl;

import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.example.volunteerend.entity.ProjectCategory;
import org.example.volunteerend.mapper.HomeMapper;
import org.example.volunteerend.mapper.ProjectCategoryMapper;
import org.example.volunteerend.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HomeServiceImpl implements HomeService {

    private final HomeMapper homeMapper;
    private final ProjectCategoryMapper projectCategoryMapper;
    private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");

    @Autowired
    public HomeServiceImpl(HomeMapper homeMapper, ProjectCategoryMapper projectCategoryMapper) {
        this.homeMapper = homeMapper;
        this.projectCategoryMapper = projectCategoryMapper;
    }

    @Override
    public Map<String, Object> getHomeData() {
        Map<String, Object> result = new HashMap<>();
        
        // 获取轮播项目
        List<Map<String, Object>> bannerProjects = homeMapper.getBannerProjects();
        result.put("banner_projects", bannerProjects);
        
        // 获取热门项目
        List<Map<String, Object>> hotProjects = homeMapper.getHotProjects();
        for (Map<String, Object> project : hotProjects) {
            // 格式化日期时间
            if (project.get("start_time") != null) {
                project.put("start_time", project.get("start_time").toString());
            }
            if (project.get("end_time") != null) {
                project.put("end_time", project.get("end_time").toString());
            }
        }
        result.put("hot_projects", hotProjects);
        
        // 获取所有分类
        List<ProjectCategory> categories = projectCategoryMapper.findAll();
        result.put("categories", categories);
        
        // 获取平台统计数据
        Map<String, Object> statistics = homeMapper.getStatistics();
        result.put("statistics", statistics);
        
        return result;
    }

    @Override
    public List<Map<String, Object>> getRecommendedProjects(Integer userId, Integer limit) {
        if (limit == null || limit <= 0) {
            limit = 5; // 默认值
        }
        
        // 获取基于用户兴趣的推荐项目
        List<Map<String, Object>> recommendedProjects = homeMapper.getRecommendedProjects(userId, limit);
        
        // 处理结果，添加推荐原因
        for (Map<String, Object> project : recommendedProjects) {
            // 格式化日期时间
            if (project.get("start_time") != null) {
                project.put("start_time", project.get("start_time").toString());
            }
            if (project.get("end_time") != null) {
                project.put("end_time", project.get("end_time").toString());
            }
            
            // 添加推荐原因
            String categoryName = (String) project.get("category_name");
            project.put("recommended_reason", "基于您对" + categoryName + "的兴趣");
            
            // 删除不需要的字段
            project.remove("category_name");
        }
        
        // 如果推荐项目不足，可以添加一些热门项目填充
        if (recommendedProjects.size() < limit) {
            List<Map<String, Object>> hotProjects = homeMapper.getHotProjects();
            
            for (Map<String, Object> hotProject : hotProjects) {
                // 检查是否已在推荐项目中
                boolean exists = false;
                for (Map<String, Object> recommended : recommendedProjects) {
                    if (recommended.get("project_id").equals(hotProject.get("project_id"))) {
                        exists = true;
                        break;
                    }
                }
                
                if (!exists) {
                    // 格式化日期时间
                    if (hotProject.get("start_time") != null) {
                        hotProject.put("start_time", hotProject.get("start_time").toString());
                    }
                    if (hotProject.get("end_time") != null) {
                        hotProject.put("end_time", hotProject.get("end_time").toString());
                    }
                    
                    // 添加推荐原因
                    hotProject.put("recommended_reason", "热门项目");
                    
                    recommendedProjects.add(hotProject);
                    
                    // 达到要求的数量后退出
                    if (recommendedProjects.size() >= limit) {
                        break;
                    }
                }
            }
        }
        
        return recommendedProjects;
    }
} 